""" System imports """
import os,sys
import logging
import unittest

""" Other modules """
import pygame

""" Game """
from constants import *
import settings
import level
import ui.mainmenu
import player
import eventrouter
import input
import inputhandler
import session
from mixboard import mixboard
import ui.widget
from ui import font, menu

""" Constants """

""" Set up logging """
_log = logging.getLogger("epicquest.test.testui")
_log.debug("Testing UI elements")

class MainMenu(session.Session):
    
    nesfont = None
    bignesfont = None
    menu = None
    
    def __init__(self):
        self.bignesfont = font.Font(NES_FONT, (255, 255, 255))
        self.bignesfont.set_scale(2)
        self.nesfont = font.Font(NES_FONT, (255, 255, 255))
        self.menu = menu.Menu(self.bignesfont, ["SECRET!","","New","Load","Exit"],3)
        self.menu.move_cursor(1)
        self.menu.move_cursor(1)
    
    def update(self):
        if inputhandler.is_pressed(DOWN):
            self.menu.move_cursor(1)
            self.update_state('menuchoice',self.menu.get_option())
        if inputhandler.is_pressed(UP):
            self.menu.move_cursor(-1)
            self.update_state('menuchoice',self.menu.get_option())
       
        # If you press start, check which option you're on!
        if inputhandler.is_pressed(START):
            option, text = self.menu.get_option()
            print "You pressed " + text
            if option == 2:
                pygame.mixer.fadeout(500)
                pygame.time.delay(500)
                return 1
            if option == 3:
                pass
            if option == 4:
                raise SystemExit
            if option == 0:
                print "Super secret! YAAAAAY!"
            
    def exit_menu(self):
        mixboard.fadeout_music(500)
    
    def draw(self, surface):
        # Render text with the font
        ren = self.bignesfont.render_center("Epic Quest", surface, False, 128)
        self.menu.draw(surface, ((SCREEN_SIZE[0] - self.menu.width) / 2, SCREEN_SIZE[1] / 2))

if __name__ == "__main__":
    
    pygame.init()
    pygame.display.set_caption(GAME_CAPTION)
    # Set up the screen
    screen = pygame.display.set_mode(SCREEN_SIZE)    
    
    clock = pygame.time.Clock()
        
    inputmanager = input.Input()
        
    #currentlevel = level.Level("level1.txt")
    #player = player.Player((1,0))
    
    #eventrouter.EventRouter().register(inputmanager, pygame.KEYDOWN)
    #eventrouter.EventRouter().register(inputmanager, pygame.KEYUP)
    #eventrouter.EventRouter().register(player, 15)
    
    #eventrouter.EventRouter().start()
    
    # Now we are ready to start the game.   

    testmenu = ui.mainmenu.MainMenu
    testmenu.start_music(os.path.join('music','ThePrelude.ogg'))
    
    current_session = testmenu
    
    while True:
        clock.tick(30)
        screen = pygame.display.get_surface()
        
        """ Clear the screen """
        screen.fill((0,0,0))
        
        """ Handle new input """
        inputhandler.handle_input()
        
        """ update the menu """
        if current_session.update() == 1:
            current_session.end_session()
        
        current_session.draw(screen)
        
        """ Update the screen and pump events """
        pygame.display.flip()
        pygame.event.pump()

